import 'package:flutter/material.dart';
import 'constants.dart';
import 'conversation_page.dart';
import 'contacts_page.dart';



/// 通过枚举的方式
enum ActionItems {
  GROUP_CHAT, ADD_FRIEND, QR_SCAN, PAYMENT, HELP
}

class NavigationIconView {

  final BottomNavigationBarItem item;

  /// 构造函数
  NavigationIconView({Key key, String title, IconData icon, IconData activeIcon}) :
        item = BottomNavigationBarItem(
          icon: Icon(icon, color: Color(AppColors.TabIconNormal)),
          activeIcon: Icon(activeIcon, color: Color(AppColors.TabIconActive)),
          title: Text(title),
          backgroundColor: Colors.white,
        );
}

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {

  PageController _pageController;
  int _currentIndex = 0;
  List<NavigationIconView> _navigationViews;
  List<Widget> _pages;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _navigationViews = [

      NavigationIconView(
          title: '微信',
          icon: IconData(
            0xe608,
            fontFamily: Constants.IconFontFamily,
          ),
          activeIcon: IconData(
            0xe603,
            fontFamily: Constants.IconFontFamily,
          ),
      ),

      NavigationIconView(
          title: '通讯录',
          icon: IconData(
            0xe601,
            fontFamily: Constants.IconFontFamily,
          ),
          activeIcon: IconData(
            0xe656,
            fontFamily: Constants.IconFontFamily,
          ),
      ),

      NavigationIconView(
          title: '发现',
          icon: IconData(
            0xe600,
            fontFamily: Constants.IconFontFamily,
          ),
          activeIcon: IconData(
            0xe671,
            fontFamily: Constants.IconFontFamily,
          ),
      ),

      NavigationIconView(
          title: '我',
          icon: IconData(
            0xe6c0,
            fontFamily: Constants.IconFontFamily,
          ),
          activeIcon: IconData(
            0xe626,
            fontFamily: Constants.IconFontFamily,
          ),
      ),

    ];

    _pageController = PageController(initialPage: _currentIndex);

    _pages = [

      ConversationPage(),
      ContactsPage(),
      Container(color: Colors.blue),
      Container(color: Colors.purple),
    ];
  }

  /// 私有方法 _
  _buildPopupMenuItem(int iconName, String title) {
    /// 行布局
    return Row(
      children: <Widget>[
        /// const 不可变
        Icon(IconData(iconName, fontFamily: Constants.IconFontFamily), size: 22.0, color: const Color(AppColors.AppBarPopupMenuTextColor)),
        Container(width: 12.0),
        Text(title, style: TextStyle(color: const Color(AppColors.AppBarPopupMenuTextColor)))
      ],
    );
  }


  @override
  Widget build(BuildContext context) {

    final BottomNavigationBar bottomNavigationBarBar = BottomNavigationBar(

      /// 底部图标高亮颜色的处理
      fixedColor: const Color(AppColors.TabIconActive),

      items : _navigationViews.map((NavigationIconView view){
        return view.item;
      }).toList(),
      currentIndex : _currentIndex,
      type : BottomNavigationBarType.fixed,
      onTap: (int index){
        print('点击的是第$index个tab');
        setState(() {
          _currentIndex = index;
          /// milliseconds 200毫秒
          _pageController.animateToPage(_currentIndex, duration: Duration(milliseconds: 200), curve: Curves.easeInOut);
        });
      },
    );

    return Scaffold(

      appBar: AppBar(
        /// AppBar下面的阴影 默认是4.0
        elevation: 0.0,
        title: Text('wechat'),
        /// 数组
        actions: <Widget>[

          /// 搜索图标
          IconButton(
            icon: Icon(
                IconData(
                  /// 暂无
                  0xe65e,
                  fontFamily: Constants.IconFontFamily,
                ),
                size: 22.0
            ),
            onPressed: (){
              print('首页右上角搜索按钮点击');
            },
          ),

          Container(width: 16),

          PopupMenuButton(
            itemBuilder: (BuildContext context) {
              return <PopupMenuItem<ActionItems>>[

                PopupMenuItem(
                  child: _buildPopupMenuItem(0xe69e, "发起群聊"),
                  value: ActionItems.GROUP_CHAT,
                ),

                PopupMenuItem(
                  child: _buildPopupMenuItem(0xe638, "添加朋友"),
                  value: ActionItems.ADD_FRIEND,
                ),

                PopupMenuItem(
                  child: _buildPopupMenuItem(0xe61b, "扫一扫"),
                  value: ActionItems.QR_SCAN,
                ),

                PopupMenuItem(
                  child: _buildPopupMenuItem(0xe62a, "收付款"),
                  value: ActionItems.PAYMENT,
                ),

                PopupMenuItem(
                  child: _buildPopupMenuItem(0xe63d, "帮助与反馈"),
                  value: ActionItems.HELP,
                ),

              ];
            },

            icon: Icon(IconData(
              0xe658,
              fontFamily: Constants.IconFontFamily,
            ), size: 22.0),

            onSelected: (ActionItems selected) {
              print('点击的是$selected');
            },

          ),

          Container(width: 16),

        ],
      ),

      body: PageView.builder(

        itemBuilder: (BuildContext context, int index) {
          return _pages[index];
        },
        controller: _pageController,
        itemCount: _pages.length,
        onPageChanged: (int index){
            print('$index');
            setState(() {
              _currentIndex = index;
            });
        },

      ),

      bottomNavigationBar: bottomNavigationBarBar,

    );
  }
}



